-- UIAttribute
-- create by daisl
-- 人物属性界面

UIAttribute = class("UIAttribute", function()
    return cc.Layer:create();
end
);

function UIAttribute.create()
    return UIAttribute.new();
end

-- 内部函数声明
local resize;
local isTouch;

-- 构造函数
function UIAttribute:ctor()
    self:setName("UIAttribute");

    --需要显示在英雄子菜单前面
    self:setLocalZOrder(UIMgr.HERO_SUBMENU_ZORDER+1);

    local node = cc.CSLoader:createNode("layout/equips/attribute.csb")
    self:addChild(node);
    self.node = node;

    local textNode = cc.CSLoader:createNode("layout/equips/attributeText.csb")
    self.widget = replaceNodeWithWidget(textNode);
    self.widget:retain();

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(event)
        if event == "exit" then
            self.widget:release();
        end
    end);

    -- 处理关闭人物属性界面
    local bg = findChildByName(node, "bg/bgImg");
    local CT = node:getChildByName("CT");
    local function onCloseAttribute(sender, eventType)
        local orbit = cc.OrbitCamera:create(0.2,1, 0, 0, 90, 0, 0);
        local scaleBy =  cc.ScaleBy:create(0.2,0.9,0.9);
        local spawn =  cc.Spawn:create(orbit,scaleBy);
        if eventType == ccui.TouchEventType.ended then
            if self.isOpen == true then
                return;
            end

            self.isOpen = true;

            -- 关闭当前界面
            local function backToUIEquip ()
                UIMgr.getCurrentScene():removeForm(self);
            end

            local function onReset ()
                self.isOpen = false;
            end

            local allAction = cc.Sequence:create(spawn,cc.CallFunc:create(backToUIEquip),cc.CallFunc:create(onReset));
            CT:runAction(allAction);

        end
    end
    bg:addTouchEventListener(onCloseAttribute);

    resize(node, textNode);
    self:initText();

end

-- 更新位置
resize = function (node, textNode)
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local topMenuHeight = UIMgr.getTopMenuHeight();
    local bottomMenuHeight = UIMgr.getBottomMenuHeight();
    local height = h - topMenuHeight;
    local heightGap = 85;

    local bg = node:getChildByName("bg");
    local oldBgHeight = bg:getContentSize().height;
    bg:setContentSize(w,height);
    bg:setPosition(cc.p(0, h - topMenuHeight+2));

    local text = findChildByName(node,"CT/text");
    TextStyleM.setOutlineStyle(text);
    text:setPositionY(text:getPositionY()-8);

    local center = node:getChildByName("CT")
    local scale = height/oldBgHeight;
    center:setPosition(cc.p(w/2, h/2 + heightGap * scale));
    center:setScale(scale);
end

-- 初始化文本内容
function UIAttribute:initText ()
    local scrollView = findChildByName(self.node,"CT/scrollview");
    local props = EquipM.fetchAllProps(ME.user)

    -- 描述
    local descTable = UserPropM.getPropDesc(props);
    local descNum = #table.keys(descTable);

    -- 设置触摸
    scrollView:setTouchEnabled(isTouch(descNum));

    local iconHeight = 42;
    local gap = 0;
    local leftMargin = 3;

    -- 计算出滚动总高度
    local totalHeight = math.max(descNum*(iconHeight+gap),
        scrollView:getInnerContainerSize().height);

    scrollView:setInnerContainerSize(cc.size(scrollView:getInnerContainerSize().width, totalHeight));

    for i=1, descNum do
        local child = self.widget:clone();
        local text = child:getChildByName("text");

        --文字描述
        applyString(text, descTable[i]);
        child:setAnchorPoint(cc.p(0, 0));
        child:setPositionX(leftMargin);
        child:setPositionY(totalHeight - i * (iconHeight + gap));
        scrollView:addChild(child);
    end
    scrollView:scrollToTop(0.1, true);
end

-- 设置滚动容器是否触摸
isTouch = function (descNum)
    if descNum <= 10 then
       return false;
    else
        return true;
    end
end
